package com.mark.web.data.service.impl;

import com.mark.common.ServerResponse;
import com.mark.web.data.mapper.AdminMapper;
import com.mark.web.data.exception.AdminException;
import com.mark.web.data.entity.po.Admin;
import com.mark.web.data.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: 后台管理员业务逻辑实现
 * @Author: Mark
 * @CreateDate: 2018/8/14.
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@Service("adminService")
public class AdminServiceImpl implements IAdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public ServerResponse login(String adminName, String password) {
        try {
            Admin admin = adminMapper.selectLogin(adminName, password);
            if (admin != null) {
                int resultCount = adminMapper.updateByPrimaryKeySelective(admin);
                if (resultCount > 0) {
                    admin.setPassword("");//为了保证安全：若登录成功将管理员的密码置为再返回到前端
                    return ServerResponse.createBySuccess("登录成功", admin);
                }
            }
            return ServerResponse.createByError();
        }catch (Exception e){
            throw new AdminException("login",e.getClass()+e.getMessage());
        }
    }
}
